﻿using AutoMapper;
using BS.Infra;
using BS.Infra.Interface;
using MediatR;

namespace BS.Write.Api.Handler.Imunities
{
    public class DelImuntityCommandHandler : IRequestHandler<DelImunityByIdCommand, ApiResult>
    {
        private readonly IImunitysRepository _immunitysRepository;

        private readonly ILogger<DelImuntityCommandHandler> _logger;

        private readonly IMapper _mapper;

        public DelImuntityCommandHandler(IImunitysRepository immunitysRepository, ILogger<DelImuntityCommandHandler> logger, IMapper mapper)
        {
            _immunitysRepository = immunitysRepository;
            _logger = logger;
            _mapper = mapper;
        }
        public async Task<ApiResult> Handle(DelImunityByIdCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var asa=await _immunitysRepository.GetAsync(x=>x.ImunityId==request.ImunityId);
                if (asa.ImunityId == null)
                {
                    return ApiResult.Fail("删除该保健信息");
                }
                asa.IsDeleted=true;
                await _immunitysRepository.UpdateAsync(asa);
                return ApiResult.Ok();
            }
            catch (Exception e)
            {
                _logger.LogError(e.Message+e.Message);
                throw;
            }
        }
    }
}
public class DelImunityByIdCommand:IRequest<ApiResult>
{
    public int ImunityId { get; set; }
}